﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Planer.Model;

namespace Planer.Logic
{
    public class PreExcecuter
    {
        public static void Excecute(user CurrentUser)
        {
            List<periodical_action> periodicalActionList = PeriodicalActionModel.GetInstance().GetPeriodicalActionsByUser(CurrentUser);

            foreach (periodical_action action in periodicalActionList)
            {
                /*
                 * 1) время транзакции еще не наступило
                 * 2) время транзакции наступило - провести
                 */
                if (action.periodical_action_transanction.Count == 0)
                {
                    //время транзакции наступило - провести                    
                    if (PeriodModel.CompareShortDates(action.apply_date, DateTime.Now) <= 0)
                    {
                        PeriodicalActionModel.GetInstance().CommitTransaction(action, action.apply_date);
                        DateTime date = PeriodModel.CreateDateFromDateAndPeriod(action.apply_date, action);
                        while (PeriodModel.CompareShortDates(date, DateTime.Now) <= 0)
                        {
                            PeriodicalActionModel.GetInstance().CommitTransaction(action, date);
                            date = PeriodModel.CreateDateFromDateAndPeriod(date, action);
                        }
                    }
                }
                else
                {
                    List<object> temp = MixedTransactionListSorter.Sort(new List<action>(), action.periodical_action_transanction.ToList());
                    periodical_action_transanction lastTransaction = (periodical_action_transanction)temp[0];
                    /*if (PeriodModel.CompareShortDates(lastTransaction.mustbe_date, lastTransaction.create_date) != 0)
                    {
                        //последняя транзакция проводилась вручную
                        DateTime periodBorder = PeriodModel.CreateDateFromDateAndPeriod(DateTime.Now, action);
                        if (PeriodModel.CompareShortDates(DateTime.Now, periodBorder) >= 0)
                        {
                            //время транзакции прошло (или пришло)
                            results.Add(PeriodicalActionModel.GetInstance().CommitTransaction(action));
                        }
                    }*/
                    DateTime date = PeriodModel.CreateDateFromDateAndPeriod(lastTransaction.mustbe_date, action);
                    while (PeriodModel.CompareShortDates(date, DateTime.Now) <= 0)
                    {
                        PeriodicalActionModel.GetInstance().CommitTransaction(action, date);
                        date = PeriodModel.CreateDateFromDateAndPeriod(date, action);
                    }
                }
            }

            List<allocation> allocationList = AllocationModel.GetInstance().GetAllocationsByUser(CurrentUser);
            foreach (allocation alloc in allocationList)
            {
                /*
                 * 1) время транзакции еще не наступило
                 * 2) время транзакции наступило - провести
                 */
                if (alloc.allocation_transanction.Count == 0)
                {
                    //время транзакции наступило - провести                    
                    if (PeriodModel.CompareShortDates(alloc.apply_date, DateTime.Now) <= 0)
                    {
                        AllocationModel.GetInstance().CommitTransaction(alloc, alloc.apply_date);
                        DateTime date = PeriodModel.CreateDateFromDateAndPeriod(alloc.apply_date, alloc);
                        while (PeriodModel.CompareShortDates(date, DateTime.Now) <= 0)
                        {
                            AllocationModel.GetInstance().CommitTransaction(alloc, date);
                            date = PeriodModel.CreateDateFromDateAndPeriod(date, alloc);
                        }
                    }
                }
                else
                {
                    List<object> temp = MixedTransactionListSorter.Sort(new List<action>(), new List<periodical_action_transanction>(), alloc.allocation_transanction.ToList());
                    allocation_transanction lastTransaction = (allocation_transanction)temp[0];
                    DateTime date = PeriodModel.CreateDateFromDateAndPeriod(lastTransaction.mustbe_date, alloc);
                    while (PeriodModel.CompareShortDates(date, DateTime.Now) <= 0)
                    {
                        AllocationModel.GetInstance().CommitTransaction(alloc, date);
                        date = PeriodModel.CreateDateFromDateAndPeriod(date, alloc);
                    }
                }
            }
        }
    }
}
